<template>
  <div class="detailtotal">
    <div class="imginfo">
      <img src="@/assets/img/coursedetail.png" alt="" />
    </div>
    <div class="detailnav">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>课程管理</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/' }">课程详情</el-breadcrumb-item>
        <el-breadcrumb-item></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="detailtable">
      <el-table :data="tableData" height="600" border style="width: 100%">
        <el-table-column prop="courseid" label="id" width="80">
        </el-table-column>
        <el-table-column prop="coursename" label="课程名" width="180">
        </el-table-column>
        <el-table-column prop="courselist" label="课程描述" width="180">
        </el-table-column>
        <el-table-column prop="publishdatatime" label="发布时间" width="180">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="danger" @click="deletecourse(scope.row.courseid)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="upmv">
      <el-button type="text" class="sccourse" @click="dialogVisible = true"
        >上传课程</el-button
      >
      <el-dialog title="上传课程" :visible.sync="dialogVisible" width="30%">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="课程描述">
            <el-input v-model="form.listname"></el-input>
          </el-form-item>
        </el-form>
        <el-upload
          class="upload-demo"
          action="http://localhost:81/teacher/upcoursev"
          :before-upload="beforeAvatarUpload"
          :on-success="handleAvatarSuccess"
          name="tvFile"
          multiple
          :limit="3"
        >
          <el-button class="ssccourse" type="primary">选择视频</el-button>
        </el-upload>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button @click="sureuptv">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  props: {
    coursename: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      form: {
        listname: '',
      },
      filename: '',
    }
  },
  created() {
    this.start()
  },
  methods: {
    // 根据课程名查询课程
    async start() {
      const { data: res } = await axios.get(
        'http://localhost:81/hcourse/getCourse',
        {
          params: {
            coursename: this.coursename,
          },
        }
      )
      if (res.code == 200) {
        console.log('查询详情成功', res.data)
        this.tableData = res.data
        // 格式化时间
        for (let i = 0; i < res.data.length; i++) {
          const newDate = new Date(res.data[i].publishdatatime).format(
            'yyyy-MM-dd hh:mm:ss'
          )
          this.tableData[i].publishdatatime = newDate
        }
      } else {
        this.$message.error(res.msg)
      }
    },
    deletecourse(courseid) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          axios
            .get('http://localhost:81/hcourse/delSingleCourse', {
              params: {
                courseid: courseid,
                coursename: this.coursename,
              },
            })
            .then((res) => {
              if (res.data.code == 200) {
                this.$message.success('删除成功')
                this.start()
              } else {
                this.$message.error(res.data.msg)
              }
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    // 上传课程
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      console.log('上传完后视频名', res)
      this.filename = res.data
    },
    beforeAvatarUpload(file) {
      var that = this
      console.log('文件', file)
      // 获取后缀名
      const suffix = file.name.substring(file.name.lastIndexOf('.') + 1)
      const isLt2M = file.size / 1024 / 1024 < 500
      const isLtType = [
        'video/mp4',
        'video/ogg',
        'video/flv',
        'video/avi',
        'video/wmv',
        'video/rmvb',
        'video/mov',
      ]

      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 500M!')
      }
      let correctType = true
      // 限制视频格式
      if (isLtType.indexOf(file.type) == -1) {
        correctType = false
        this.$message.warning('请上传视频文件')
      } else {
        correctType = true
      }

      // 获取视频时长
      let url = URL.createObjectURL(file)
      let audioElement = new Audio(url)
      let duration
      let fun = (duration) => {
        this.duration = duration
      }
      audioElement.addEventListener('loadedmetadata', function () {
        duration = audioElement.duration //以秒为单位
        fun(parseFloat(duration).toFixed(1))
      })

      return isLt2M && correctType
    },
    // 确定上传视频
    async sureuptv() {
      console.log('上传视频数据', this.form)
      console.log('视频名', this.filename)
      const { data: res } = await axios.get(
        'http://localhost:81/teacher/upCourse',
        {
          params: {
            listname: this.form.listname,
            coursename: this.coursename,
            mvname: this.filename,
          },
        }
      )
      if (res.code == 200) {
        this.dialogVisible = false
        this.form = {}
        this.$message.success('上传成功')
        this.start()
      } else {
        this.$message.error(res.data.msg)
      }
    },
  },
}
</script>

<style lang="less" scoped>
.detailtotal {
  position: relative;
  .imginfo {
    img {
      width: 100%;
      height: 100%;
      opacity: 0.2;
    }
  }
  .detailnav {
    position: absolute;
    top: 20px;
    left: 20px;
  }
  .detailtable {
    position: absolute;
    top: 100px;
    left: 10%;
    width: 80%;
  }
  .upmv{
    position: absolute;
    top: 600px;
    left: 45%;
    width: 10%;
    .ssccourse{
      position: absolute;
      bottom: 20px;
      left: 50px;
    }
  }
}
</style>
